package games.gong.durid.controller;

import games.gong.durid.common.R;
import games.gong.durid.common.PermissionConstants;
import games.gong.durid.dto.RoleDTO;
import games.gong.durid.entity.Role;
import games.gong.durid.service.RoleService;
import games.gong.durid.vo.RoleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Tag(name = "角色管理", description = "角色管理接口")
@RestController
@RequestMapping("/api/roles")
public class RoleController {
    
    @Autowired
    private RoleService roleService;
    
    @Operation(summary = "创建角色")
    @PostMapping
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.ROLE_CREATE + "')")
    public R<RoleVO> createRole(
            @Parameter(description = "角色信息") @RequestBody RoleDTO roleDTO) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO, role);
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        int result = roleService.addRole(role);
        if (result > 0) {
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(role, roleVO);
            return R.success("角色创建成功", roleVO);
        } else {
            return R.error("角色创建失败");
        }
    }
    
    @Operation(summary = "删除角色")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.ROLE_DELETE + "')")
    public R<String> deleteRole(
            @Parameter(description = "角色ID") @PathVariable Long id) {
        int result = roleService.deleteRole(id);
        if (result > 0) {
            return R.success("角色删除成功");
        } else {
            return R.error("角色删除失败");
        }
    }
    
    @Operation(summary = "更新角色")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.ROLE_UPDATE + "')")
    public R<RoleVO> updateRole(
            @Parameter(description = "角色ID") @PathVariable Long id,
            @Parameter(description = "角色信息") @RequestBody RoleDTO roleDTO) {
        Role role = new Role();
        BeanUtils.copyProperties(roleDTO, role);
        role.setId(id);
        role.setUpdateTime(LocalDateTime.now());
        int result = roleService.updateRole(role);
        if (result > 0) {
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(role, roleVO);
            return R.success("角色更新成功", roleVO);
        } else {
            return R.error("角色更新失败");
        }
    }
    
    @Operation(summary = "获取角色详情")
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.ROLE_READ + "')")
    public R<RoleVO> getRole(
            @Parameter(description = "角色ID") @PathVariable Long id) {
        Role role = roleService.getRoleById(id);
        if (role != null) {
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(role, roleVO);
            return R.success(roleVO);
        } else {
            return R.error("角色不存在");
        }
    }
    
    @Operation(summary = "获取所有角色")
    @GetMapping
    @PreAuthorize("@ss.hasPermission('" + PermissionConstants.ROLE_READ + "')")
    public R<List<RoleVO>> getAllRoles() {
        List<Role> roles = roleService.getAllRoles();
        List<RoleVO> roleVOs = roles.stream().map(role -> {
            RoleVO roleVO = new RoleVO();
            BeanUtils.copyProperties(role, roleVO);
            return roleVO;
        }).collect(Collectors.toList());
        return R.success(roleVOs);
    }
}